﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.StockWarning
{
    /// <summary>
    /// 设置库存预警值
    /// </summary>
    public partial class SetStockWarningView : UserControl, IView
    {
        Promise _promise = new Promise();
        /// <summary>
        /// 选择的商品
        /// </summary>
        WebRef.VStockMaterial itemVMaterial = null;
        public SetStockWarningView()
        {
            InitializeComponent();
            ValidationManager.Register(this,new Common.Validations.SearchLookUpEditValidation(this.ctlSearchLookUpWareHouse, "仓库"));
            ValidationManager.Register(this, new Common.Validations.TextEditIsFloatNumber(this.ctltxtWarmingNum, "预警数量", false));
        }

        #region IView 成员
        public ViewHeader GetHeader()
        {
            return new ViewHeader("设置库存预警值");
        }
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", Resources.isOK, "保存", OnSaveClick))
              .AddAction(new ActionNode("cancel", Resources.isDelete, "取消", OnCancelClick));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEWarehouse(this.ctlSearchLookUpWareHouse, _promise, null, true);
            }).Run();
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.View is Pickers.MaterialStockPicker)
                {
                    itemVMaterial = args.ClosedArgs.Data as WebRef.VStockMaterial;
                    if (itemVMaterial != null)
                    {
                        this.ctlBtnQryMaterial.Text = itemVMaterial.Name;
                    }
                }
            }
        }
        #endregion

        #region Method
        /// <summary>
        /// 数据检查
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            string strMess = null;
            if (!ValidationManager.Validate(this, out strMess))
            {
                UIContext.Current.ViewManager.ShowWarning(strMess);
                return false;
            }
            if (itemVMaterial == null)
            {
                UIContext.Current.ViewManager.ShowWarning("请选择商品！");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 预警数量设置
        /// </summary>
        private void CreateStockWarmingValue()
        {
            WebRef.EStockThresholdValue entity = new WebRef.EStockThresholdValue();
            entity.WarehouseId = ctlSearchLookUpWareHouse.GetSelectedValue<WebRef.VWarehouse>().Id;
            entity.MaterialId = itemVMaterial.Id;
            entity.Value = Convert.ToDecimal(this.ctltxtWarmingNum.Text.Trim());

            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ServiceResult result = new Service.CreateService().CreateStockThresholdValue(entity);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, null);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnSaveClick(object obj1, object obj2)
        {
            if (ValidateData())
            {
                CreateStockWarmingValue();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnCancelClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.CloseView(true, null);
        }
        /// <summary>
        /// 选择商品&清除商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnQryMaterial_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.Equals("clear"))
            {
                this.ctlBtnQryMaterial.Text = "";
                itemVMaterial = null;
            }
            else
            {
                WebRef.VWarehouse vWarehouse = ctlSearchLookUpWareHouse.GetSelectedValue<WebRef.VWarehouse>();
                if (vWarehouse != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Pickers.MaterialStockPicker(vWarehouse.Id),
                        new PopupViewArgs("按仓库选择商品"));
                }
                else
                {
                    UIContext.Current.ViewManager.ShowInfo("请选择仓库！");
                }
            }
        }
        #endregion

    }
}
